<template>
	<view class="task_detail">
		<view class="top-nav">
			<view @click="backBtn">返回</view>
			<view>任务详情</view>
			<view></view>
		</view>
		<view class="main">
			<view class="main-content">
				<view>任务状态：</view>
				<view>{{taskDetail.auditStatus}}</view>
			</view>
			<view class="main-content">
				<view>任务编号：</view>
				<view>{{taskDetail.serialNo}}</view>
			</view>
			<view class="main-content">
				<view>位置：</view>
				<view>{{taskDetail.address}}</view>
			</view>
			<view class="rich-content">
				<rich-text :nodes="taskDetail.material"></rich-text>
			</view>
			<view class="main-content main-item" style="justify-content: left;margin: 0;">
				<view class="btn-currency" style="margin: 0;width: 150rpx;" @click="signUp()">{{taskDetail.signUp == '0'? '我要报名':taskDetail.signUp == '3'?'人数已满':'已报名'}}</view>
			</view>
			<view class="main-content">
				<view>发布人：</view>
				<view>{{taskDetail.createBy}}</view>
			</view>
			<view class="main-content">
				<view>发布时间：</view>
				<view>{{taskDetail.createTime}}</view>
			</view>
			<view class="main-content gray" v-if="signInfo">
				<view v-if="signInfo.status == 1">{{signInfo.createTime.split(' ')[0]}} : {{signInfo.username}}已报名</view>
				<view v-if="signInfo.status == 2">{{signInfo.createTime.split(' ')[0]}} : {{signInfo.username}}已通过审核</view>
			</view>
		<view class="main-content">
				<view style="margin-right: 20rpx;">已报名人数：{{taskDetail.registeredNum}}</view>
				<view>已参加人数：{{taskDetail.participateInNum}}</view>
		</view>
		<view class="main-content" v-if="openId == taskDetail.openId">
			<view class="button btn-currency" @click="goExamine" :data-id=taskDetail.id>人员审核</view>
			<view class="button btn-currency" @click="taskState(4)">开始任务</view>
			<view class="button btn-currency" @click="taskState(5)">结束任务</view>
		</view>
	</view>
	<view class="main">
		<view class="main-content" style="justify-content: space-between;">
			<view>已确认参与的志愿者</view>
			<!-- <view>展开</view> -->
		</view>
		<view class="main-list">
			<view v-for="(item,index) in userList" :key="index">
				<image :src="item.avatar_url" mode=""></image>
				<view>{{item.username}}</view>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				taskId: '',
				openId: 'ogiDYs4eDr9DA2pofZeYsFQygtt4',
				// baseUrl: 'http://192.168.3.23:8085/jeecg-boot/',
				baseUrl: 'http://weixiaokong.91xiaokong.com/jeecg-boot/',
				taskDetail: {},
				signInfo: {},
				userList: [],
			}
		},
		onLoad(option) {
			_this = this;
			_this.taskId = option.id;
			let openid = uni.getStorageSync('openid')
			if (openid) {
				_this.openId = openid
			}
		},
		onShow() {
			// 获取任务信息
			uni.request({
				url: _this.baseUrl + 'rescue/hnjzRescueInformation/queryById',
				data: {
					id: _this.taskId,
					openId: _this.openId
				},
				success(res) {
					_this.taskDetail = res.data.result
				}
			})
			// 当前报名状态
			uni.request({
				url: _this.baseUrl + 'api/rescue/query/user/records/',
				header: {
					'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
				},
				data: {
					infoId: _this.taskId,
					openId: _this.openId
				},
				success(res) {
					_this.signInfo = res.data.result
				}
			})
			// 所有报名用户集合
			uni.request({
				url: _this.baseUrl + 'api/rescue/infoUserList',
				data: {
					infoId: _this.taskId,
					openId: _this.openId
				},
				success(res) {
					// _this.userList = res.data.result
					_this.userList = []
					if (res.data.result && res.data.result.length > 0) {
						res.data.result.forEach(function(e) {
							if (e.status == 2) {
								_this.userList.push(e)
							}
						})
					}
				}
			})
		},
		methods: {
			backBtn() {
				uni.navigateBack({
					delta: 1
				})
			},
			goExamine(e) {
				uni.navigateTo({
					url: '../user_examine/user_examine?id=' + e.currentTarget.dataset.id
				})
			},
			signUp() {
				if (_this.taskDetail.signUp == '0') {
					uni.request({
						url: _this.baseUrl + 'api/rescue/signUp',
						method: 'POST',
						header: {
							'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
						},
						data: {
							infoId: _this.taskId,
							openId: _this.openId,
						},
						success(res) {
							if (res.data.code == 200) {
								alert('报名成功')
								uni.request({
									url: _this.baseUrl + 'rescue/hnjzRescueInformation/queryById',
									data: {
										id: _this.taskId,
										openId: _this.openId
									},
									success(res) {
										_this.taskDetail = res.data.result
									}
								})
							}
							if (res.data.code == 500) {
								alert(res.data.message)
							}
						}
					})
				}
			},
			// 切换任务状态
			taskState(e) {
				uni.request({
					url: _this.baseUrl + 'rescue/hnjzRescueInformation/update/status',
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
					},
					data: {
						id: _this.taskId,
						openId: _this.openId,
						status: e
					},
					success(res) {
						if (res.data.code == 200) {
							alert('修改成功')
						}
						if (res.data.code == 500) {
							alert(res.data.message)
						}
					}
				})
			},

		},
	}
</script>

<style>
	.task_detail {
		padding-top: 100rpx;
	}

	.top-nav {
		line-height: 100rpx;
		display: flex;
		justify-content: space-between;
		background-color: #FFFFFF;
		margin-bottom: 20rpx;
		padding: 0 5%;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		background-color: #FFFFFF;
		z-index: 1000;
	}

	.main {
		width: 88%;
		margin: 30rpx auto;
		box-shadow: 0px 0px 30px 0px rgb(0 0 0 / 10%);
		border-radius: 20rpx;
		padding: 30rpx;
		background-color: #fff;
		border: 2rpx #706d6c;
	}

	.main-item view,
	.button {
		width: 150rpx;
		height: 60rpx;
		line-height: 40rpx;
		margin-top: 40rpx;
		margin-left: 50rpx;
		border-radius: 30rpx;
		font-size: 30rpx;
		padding: 10rpx;
		text-align: center;
		margin: auto;
	}

	.btn-currency {
		background-color: #b30002;
		color: #FFFFFF;
		border: none;
	}

	.main-content {
		display: flex;
		padding: 20rpx 0;
	}

	.gray {
		color: #999999;
	}

	.main-content view:first-of-type {
		white-space: nowrap;
	}

	.main-list {
		display: flex;
		justify-content: center;
		flex-wrap: wrap;
	}

	.main-list>view {
		width: 20%;
		text-align: center;
	}

	.main-list image {
		width: 60rpx;
		height: 60rpx;
	}
</style>
